package hhm.wcnmd.core.utils;

import hhm.wcnmd.core.Constants;

public class CmdStringUtils {

	/**
	 * 清除掉开头重复的命令
	 * 
	 * @param str
	 * @param promt
	 * @return
	 */
	public static String clearHeaderCmdStr(String str, String cmdStr) {
		return str.replaceFirst(".*?" + escapeExprSpecialWord(cmdStr), "");
	}

	public static String getPrompt(String str) {

		int i = str.trim().lastIndexOf(Constants.LINE_BREAK_WINDOWS);
		if (i != -1) {
			str = str.substring(i + Constants.LINE_BREAK_WINDOWS.length(), str.length());

		}

		return str.replaceAll(Constants.LINE_BREAK_WINDOWS, "");

	}

	/**
	 * 判断是否命令处于结束状态
	 * 
	 * @param str
	 * @param prompt
	 * @return
	 */
	public static boolean isEnd(String str, String prompt) {

		return getPrompt(str).equals(prompt);

	}

	public static void main(String[] args) {
		String a = "Welcome to Microsoft Telnet Service \r\n\n\rlogin: hhm\n\rpassword: yanchengjia\r\n\r\n*===============================================================\r\nMicrosoft Telnet Server.\r\n*===============================================================\r\nC:\\Users\\hhm>";
		System.out.println(getPrompt(a));
	}

	private static String escapeExprSpecialWord(String keyword) {
		if (org.apache.commons.lang.StringUtils.isNotBlank(keyword)) {
			String[] fbsArr = { "\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|" };
			for (String key : fbsArr) {
				if (keyword.contains(key)) {
					keyword = keyword.replace(key, "\\" + key);
				}
			}
		}
		return keyword;
	}

}
